<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="采购合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseNum']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseName']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseType']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseMoney']" disabled><span slot="addonAfter">元</span></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="付款条件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['payCondition', validatorRules.payCondition]"
                :disabled="model.type != 'edit'"
                placeholder="请输入付款条件"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                type="number"
                v-decorator="['expectMoney', validatorRules.expectMoney]"
                :disabled="model.type != 'edit'"
              >
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="计划付款时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择计划付款时间"
                v-decorator="['expectTime', validatorRules.expectTime]"
                :trigger-change="true"
                :disabled="model.type != 'edit'"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <!-- 2021-8-5添加开始 -->
          <a-col :span="12">
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bankDeposit']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['billCompany']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['taxNum']" disabled></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="收票日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择收票日期"
                v-decorator="['receiveBilllingTime']"
                disabled
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入发票金额"
                v-decorator="['invoicedAmount']"
                disabled
                type="number"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="是否收票" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-switch v-decorator="['billing']" disabled></j-switch>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['accessories']" :trigger-change="true" disabled></j-upload>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="特殊说明" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['remarks']" rows="4" disabled placeholder="请输入特殊说明" />
            </a-form-item>
          </a-col>
          <!-- 2021-8-5添加结束 -->

          <!-- <a-col :span="12">
            <a-form-item label="开票比例" :labelCol="labelCol" disabled :wrapperCol="wrapperCol">
              <a-input-number
                v-decorator="['billingRatio', validatorRules.billingRatio]"
                placeholder="请输入开票比例"
                style="width: 100%"
                :disabled="model.type != 'sure'"
              />
            </a-form-item>
          </a-col> -->
          <!-- <a-col :span='12'>
            <a-form-item label='回款负责人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['paymantCharger']" disabled></a-input>
            </a-form-item>
          </a-col> -->

          <a-col :span="12">
            <a-form-item label="实际付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                type="number"
                v-decorator="['actualMoney', validatorRules.actualMoney]"
                placeholder="请输入实际付款金额"
                :disabled="model.type != 'sure'"
              >
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际付款时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择实际付款时间"
                v-decorator="['actualTime', validatorRules.actualTime]"
                :trigger-change="true"
                style="width: 100%"
                :disabled="model.type != 'sure'"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="付款完成" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-switch v-decorator="['paymentCompleted']" :disabled="model.type != 'sure'"></j-switch>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'ContractPurchasePaymentForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        payCondition: { rules: [{ required: true, message: '请输入付款条件!' }] },
        expectMoney: {
          rules: [
            { required: true, message: '请输入计划付款金额!' },
            { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' },
          ],
        },
        expectTime: {
          rules: [{ required: true, message: '请选择计划付款时间!' }],
        },
        actualMoney: {
          rules: [
            { required: true, message: '请输入实际付款金额!' },
            { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' },
          ],
        },
        actualTime: {
          rules: [{ required: true, message: '请选择实际付款时间!' }],
        },
        billingRatio: {
          rules: [{ required: false }, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }],
        },
      },
      url: {
        add: '/contract/contractPurchasePayment/add',
        edit: '/contract/contractPurchasePayment/edit',
        queryById: '/contract/contractPurchasePayment/queryById',
        findByPaymentId: '/contract/contractPurchaseApplyPayment/findByPaymentId',
      },
      fields: [
        'payCondition',
        'expectMoney',
        'actualMoney',
        'expectTime',
        'actualTime',
        'paymentCompleted',
        'billingRatio',
        'contractId',
        'purchaseName',
        'purchaseNum',
        'supplierName',
        'contractSaleName',
        'contractSaleNum',
        'customerName',
        'opportunityName',
        'opportunityNum',
        'createBy',
        'purchaseMoney',
        'purchaseType',
      ],
      applyFields: [
        'bankDeposit',
        'billCompany',
        'taxNum',
        'receiveBilllingTime',
        'invoicedAmount',
        'billing',
        'accessories',
        'remarks',
      ],
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  watch: {
    'model.id': {
      deep: true,
      handler(value) {
        this.loadData(value)
      },
    },
  },
  methods: {
    loadData(value) {
      getAction(this.url.findByPaymentId, { id: value }).then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(res.result, this.applyFields))
          })
        }
      })
    },
    add() {
      this.edit({})
    },

    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.operaType()
      this.model.purchaseType = filterDictTextByCache('icm_contract_purchase_type', this.model.purchaseType)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
      if (this.model.type === 'sure') {
        this.$nextTick(() => {
          this.form.setFieldsValue({
            actualTime : this.model.actualTime ? this.model.actualTime :  this.getNowDate(),
            actualMoney : this.model.actualMoney ? this.model.actualMoney : 0,
          })
        })
      }
    },
    getNowDate() {
      let date = new Date()
      let res = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
      return res
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    operaType() {
      if (this.model.type == 'edit') {
        this.validatorRules.payCondition.rules[0].required = true
        this.validatorRules.expectMoney.rules[0].required = true
        this.validatorRules.expectTime.rules[0].required = true
        this.validatorRules.actualMoney.rules[0].required = false
        this.validatorRules.actualTime.rules[0].required = false
      }
      if (this.model.type == 'sure') {
        this.validatorRules.payCondition.rules[0].required = false
        this.validatorRules.expectMoney.rules[0].required = false
        this.validatorRules.expectTime.rules[0].required = false
        this.validatorRules.actualMoney.rules[0].required = true
        this.validatorRules.actualTime.rules[0].required = true
      }
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  },
}
</script>